
package kaizone.haining.chaobohui.fragment;

import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.DetailActivity;
import kaizone.haining.chaobohui.json.EntityHuodongDetail;
import kaizone.haining.chaobohui.json.Item;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.json.JSONResponse;
import kaizone.haining.chaobohui.json.SubjectItem;
import kaizone.haining.chaobohui.login.FollowControl;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.util.Utils;
import kaizone.haining.chaobohui.widget.DetailGridView;
import kaizone.haining.chaobohui.widget.DetailImageGallery;
import kaizone.haining.chaobohui.widget.DetailItemCard;
import kaizone.haining.chaobohui.widget.DetailTwoText;
import kaizone.haining.chaobohui.widget.ImageWindow;
import kaizone.haining.chaobohui.widget.UsersActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

public class DetailContent extends Fragment implements DataControl.IDataUpdateListener {

    public static final String ID_FLAG = "id_flag";

    public static final String DATA_SOURCE_URL = "http://api.dp-app.com/chao/remote/rest/query_activity_detail.htm?id=";

    public static final String FOLLOW_URL = "http://api.dp-app.com/chao/user/remote/rest/follow_activity.htm";

    private DetailItemCard mCard;

    private DetailTwoText mIntroTwoText01;

    private DetailTwoText mIntroTwoText02;
    
    private TextView mTextView03;

    private DetailImageGallery mGallery;

    private DetailGridView mGridView;

    private ImageFetcher mImageFetcher;

    private Object mData;

    private String mDataSourceUrl;

    private DataControl mDataControl;

    private int mRequestId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRequestId = getArguments() != null ? getArguments().getInt(ID_FLAG) : 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mImageFetcher = ((DetailActivity) getActivity()).getImageFetcher();

        View view = (View) inflater.inflate(R.layout.detail_content, null);
        mCard = (DetailItemCard) view.findViewById(R.id.card);
        mGallery = (DetailImageGallery) view.findViewById(R.id.image_gallery);
        mGallery.setImageItemClickListener(new DetailImageGallery.OnImageItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id,
                    String imageUrl) {
                showBitImage(imageUrl);
            }
        });

        mIntroTwoText01 = (DetailTwoText) view.findViewById(R.id.two_text_01);
        mIntroTwoText02 = (DetailTwoText) view.findViewById(R.id.two_text_02);
        mTextView03 = (TextView) view.findViewById(R.id.one_text_03);
        mGridView = (DetailGridView) view.findViewById(R.id.grid);

        mCard.setImageFetcher(mImageFetcher);
        mGallery.setImageFetcher(mImageFetcher);
        mGridView.setImageFetcher(mImageFetcher);

        // mCard.setOnClickListener(new DetailItemCard.OnClickListener() {
        // @Override
        // public void onButtonClick(View v) {
        // if (mData == null)
        // return;
        // if (!(mData instanceof Item))
        // return;
        // final Item item = (Item) mData;
        // final int id = item.getId();
        // // String followstring = mFollowControl.requestString(FOLLOW_URL,
        // id);
        // // mDataControl.exec(followstring);
        // }
        // });

        mDataControl = new DataControl(getActivity());
        mDataControl.addChangeListener(this);
        initdata();
        return view;
    }

    private void initdata() {
    	String number = Utils.randomNum();
        mDataSourceUrl = DATA_SOURCE_URL + mRequestId+"&t="+number;
        mDataControl.exec(mDataSourceUrl);
        initWidget();
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    private void initWidget() {
        mIntroTwoText01.setText01("活动介绍");
        mIntroTwoText02.setText01("其他介绍");
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        if (TextUtils.isEmpty(datastring))
            return null;
        Object obj = null;
        if (requeststring.equals(mDataSourceUrl)) {
            EntityHuodongDetail huodongDetail = EntityHuodongDetail.parseJson(datastring);
            obj = huodongDetail;
        }
        //
        else if (requeststring.equals(FOLLOW_URL)) {
            JSONResponse jsonResponse = JSONResponse.parseJsonString(datastring);
            obj = jsonResponse;
        }
        return obj;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result == null)
            return;
        if (result instanceof EntityHuodongDetail) {
            EntityHuodongDetail detail = (EntityHuodongDetail) result;
            final Item data = detail.getData();
            mData = data;
            if (data != null) {
                mCard.setData(data);
                List<PicModel> pic = data.getPicModelList();
                mGallery.setData(pic);
                String hdjs = data.getIntro();
                mIntroTwoText01.setText02(hdjs);
                String qtjs = data.getCountesyReminder() + "\n" + data.getNotice();
                if(qtjs.equals("\n")){
                	mIntroTwoText02.setVisibility(View.GONE);
                }else{                	
                	mIntroTwoText02.setText02(qtjs);
                }
                

                SubjectItem item = (SubjectItem) data.getSubject();
                if(item != null){
                	mTextView03.setVisibility(View.VISIBLE);
                	mGridView.setData(item);
                }else{
                	mTextView03.setVisibility(View.GONE);
                }

                UsersActionBar.attchShareData(getActivity(), data.getName(), pic);
            }
        }
        //
        if (requeststring == null)
            return;
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    public void requestAttention(int id, String userName, String userAuth) {
        final String httpstring = FOLLOW_URL;
        StringBuffer sb = new StringBuffer();
        sb.append(httpstring).append("?");
        sb.append("userName").append(userName).append("&");
        sb.append("userAuth").append(userAuth).append("&");
        sb.append("id").equals(id);

        String url = sb.toString();
        mDataControl.exec(url);
    }

    public Object getData() {
        return mData;
    }

    public void setData(Object data) {
        this.mData = data;
    }

    public int getRequestId() {
        return mRequestId;
    }

    public void setRequestId(int id) {
        this.mRequestId = id;
    }

    public void showBitImage(String imageUrl) {
        ImageWindow window = new ImageWindow(getActivity());
        window.showAtLocation(getView(), Gravity.CENTER, 0, 0);
        window.loadImage(imageUrl, mImageFetcher);
    }

    public static DetailContent newInstance(int id) {
        final DetailContent f = new DetailContent();

        final Bundle args = new Bundle();
        args.putInt(ID_FLAG, id);
        f.setArguments(args);

        return f;
    }

}
